package com.blog.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.blog.model.Message;
import com.blog.model.UserPoJo;
import com.blog.service.impl.UserHolder;
import com.blog.service.interfaces.IMessageService;
import com.blog.service.interfaces.IUserService;

@Controller
public class UserCenterController extends BaseTools{
	 
	@Autowired
	IUserService userService;
	@Autowired 
	IMessageService messageService;
	@Autowired
	UserHolder userHolder;
	@Autowired
	UserPoJo userPoJo;
	
	@RequestMapping("/usercenter/index")
	public String sadStrineg(Model model,HttpServletRequest request){
		if(SecurityContextHolder.getContext().getAuthentication().getPrincipal()!="anonymousUser"){
			UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
				    .getAuthentication()
				    .getPrincipal();
			userPoJo=userService.getUserByName(userDetails.getUsername());
		}
		model.addAttribute("user",userPoJo);//当前用户
		
		Map<Message, String> lm=messageService.selectByUserIdLimit(userPoJo.getId(),3);
		model.addAttribute("messages",lm);//用户的简要消息
		
		if(checkMobile(request.getHeader("USER-AGENT"))){
			return "usercenterMobile/index";
		}else{
			return "usercenter/index";
		}

}
 
	//用户信息
	@RequestMapping("/usercenter/account")
	public String s2Strineg(Model model,HttpServletRequest request){
		
		String str=null;
		String group=null;
		
		if(SecurityContextHolder.getContext().getAuthentication().getPrincipal()!="anonymousUser"){
			UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
				    .getAuthentication()
				    .getPrincipal();
			str="UserName"+userDetails.getUsername()+"<br>PW:"+userDetails.getPassword()+"<br>OUTDATE1:"+userDetails.isAccountNonExpired()+"<br>OUTDATE2:"+userDetails.isCredentialsNonExpired()+"<br>OUTDATE3："+userDetails.isEnabled();

			userPoJo=userService.getUserByName(userDetails.getUsername());
			 //现在只有userDetails，包含名称，token，权限等级等信息。
			//用户信息需要根据用户名搜索 保存在userPoJo
			//str=userDetails.getUsername()+userDetails.getPassword()+userDetails.isAccountNonExpired();
		}else{
			str="NO LOGIN";
		}
		str=str+"<br>PreMIssion:"+SecurityContextHolder.getContext().getAuthentication()+"<br>ALL:"+SecurityContextHolder.getContext();
		model.addAttribute("data",str);
		
		str= SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
		if(str.indexOf("ROLE_ADMIN")!=-1){
			group="管理员";
		}else if(str.indexOf("ROLE_USER")!=-1){
			group="普通用户";
		}else{
			group="游客,WARN:BUG FOUND";
		}
		
		model.addAttribute("user",userPoJo);
		model.addAttribute("group",group);
		
		if(checkMobile(request.getHeader("USER-AGENT"))){
			return "usercenterMobile/account";
		}else{
			return "usercenter/account";
		}

}
	
	
	//投稿页面
	@RequestMapping("/usercenter/submit")
	public String s3Strineg(Model model,HttpServletRequest request){
		if(SecurityContextHolder.getContext().getAuthentication().getPrincipal()!="anonymousUser"){
			UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
				    .getAuthentication()
				    .getPrincipal();
			userPoJo=userService.getUserByName(userDetails.getUsername());
		}
		model.addAttribute("user",userPoJo);
		
		if(checkMobile(request.getHeader("USER-AGENT"))){
			return "usercenterMobile/submit";
		}else{
			return "usercenter/submit";
	    }
}
	
	//反馈页面
	@RequestMapping("/usercenter/talkToMaster")
	public String s4Strineg(Model model,HttpServletRequest request){
		if(SecurityContextHolder.getContext().getAuthentication().getPrincipal()!="anonymousUser"){
			UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
				    .getAuthentication()
				    .getPrincipal();
			userPoJo=userService.getUserByName(userDetails.getUsername());
		}
		model.addAttribute("user",userPoJo);
		
		if(checkMobile(request.getHeader("USER-AGENT"))){
			return "usercenterMobile/talkToMaster";
		}else{
			//未完成 pc版的 反馈页面
			return "usercenter/submit";
	    }
}
	
	//推荐页面
	@RequestMapping("/usercenter/recommendToFriend")
	public String s6Strineg(Model model,HttpServletRequest request){
			if(SecurityContextHolder.getContext().getAuthentication().getPrincipal()!="anonymousUser"){
				UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
					    .getAuthentication()
					    .getPrincipal();
				userPoJo=userService.getUserByName(userDetails.getUsername());
			}
			model.addAttribute("user",userPoJo);
			
			if(checkMobile(request.getHeader("USER-AGENT"))){
				return "usercenterMobile/recommendToFriend";
			}else{
				//未完成 pc版的 推荐页面
				return "usercenter/submit";
		    }
	}
	
	
	//消息中心
	@RequestMapping("/usercenter/message")
	public String s33Strineg(Model model,HttpServletRequest request){
		if(SecurityContextHolder.getContext().getAuthentication().getPrincipal()!="anonymousUser"){
			UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
				    .getAuthentication()
				    .getPrincipal();
			userPoJo=userService.getUserByName(userDetails.getUsername());
		}
		
		
		List<Message> lm=messageService.selectByUserId(userPoJo.getId());
		
		model.addAttribute("user",userPoJo);
		model.addAttribute("messages",lm);
		
		if(checkMobile(request.getHeader("USER-AGENT"))){
			return "usercenterMobile/message";
		}else{
			return "usercenter/message";
	    }
		}
	
	

	
	
	
	
	
	
	
	
	
	
	
	//以下接口 废弃
	
	@RequestMapping("/userInfo")
	@ResponseBody
	public String sadString(){
		String str=null;
		System.out.println("现在"+SecurityContextHolder.getContext());
		System.out.println("现在"+SecurityContextHolder.getContext().getAuthentication());
		System.out.println("现在"+SecurityContextHolder.getContext().getAuthentication().getPrincipal());
		
		if(SecurityContextHolder.getContext().getAuthentication().getPrincipal()!="anonymousUser"){
			UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
				    .getAuthentication()
				    .getPrincipal();
			str="UserName"+userDetails.getUsername()+"<br>PW:"+userDetails.getPassword()+"<br>OUTDATE1:"+userDetails.isAccountNonExpired()+"<br>OUTDATE2:"+userDetails.isCredentialsNonExpired()+"<br>OUTDATE3："+userDetails.isEnabled();
			
			//str=userDetails.getUsername()+userDetails.getPassword()+userDetails.isAccountNonExpired();
		}else{
			str="NO LOGIN";
		}
		str=str+"<br>PreMIssion:"+SecurityContextHolder.getContext().getAuthentication()+"<br>ALL:"+SecurityContextHolder.getContext();
		return str;
	}
	
	
	@RequestMapping("/userInfo2")
	public String sadString2(Model model){
		String name = userHolder.getUserName();
		if(name!="anonymous"){
			model.addAttribute("user",userService.getUserByName(name));
		}else{
			model.addAttribute("user",userPoJo.setGroup(99));
		}
		model.addAttribute("user2",2);
		
		
		return "userCenter";}
}
